package oa.struts.action;

import java.io.File;
import java.io.InputStream;
import java.util.Collection;

import javax.annotation.Resource;

import org.jbpm.api.ProcessDefinition;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import oa.domain.page.PageBean;
import oa.domain.workflow.FormTemplate;
import oa.service.FormTemplateService;
import oa.service.PDManager;
import oa.struts.action.base.BaseAction;
import oa.util.HQLHelper;
import oa.util.UploadUtils;

@Controller("formTemplateAction")
@Scope("prototype")
public class FormTemplateAction extends BaseAction<FormTemplate>{

	@Resource(name="formTemplateService")
	private FormTemplateService formTemplateService;
	@Resource(name="pdManager")
	private PDManager pdManager;
	
	private File resource;
	
	private InputStream inputStream;
	
	
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public File getResource() {
		return resource;
	}

	public void setResource(File resource) {
		this.resource = resource;
	}

	public String showAllFormTemplate(){
		Collection<FormTemplate> ftList = this.formTemplateService.getAllFormtemplate();
		ActionContext.getContext().put("ftList", ftList);
//		HQLHelper hh = new HQLHelper(FormTemplate.class);
//		PageBean pageBean = this.formTemplateService.getPageBean(hh,currentPage);
//        ActionContext.getContext().getValueStack().push(pageBean);
		return listAction;
	}
	
	/**
	 * 跳转到表单模板的添加页面
	 * @return
	 */
	public String addUI(){
		Collection<ProcessDefinition> pdList = this.pdManager.getLasterVersion();
		ActionContext.getContext().put("pdList", pdList);
		return addUI;
	}
	
	public String add(){
		/**
		 * 1、把表单模板上传到服务器上
		 * 2、在表单模板中插入一行数据
		 */
		String url = UploadUtils.saveUploadFile(this.resource,"/WEB-INF/upload");
		FormTemplate formTemplate = new FormTemplate();
//		BeanUtils.copyProperties(this.getModel(), formTemplate);
		formTemplate.setName(this.getModel().getName());
		formTemplate.setProcessKey(this.getModel().getProcessKey());	
		formTemplate.setUrl(url);
		this.formTemplateService.saveFormtemplate(formTemplate);
		return action2action;
	}
	
	public String updateUI(){
		FormTemplate formTemplate = this.formTemplateService.getFormTemplateById(this.getModel().getFtid());
		ActionContext.getContext().getValueStack().push(formTemplate);
		Collection<ProcessDefinition> pdList = this.pdManager.getLasterVersion();
		ActionContext.getContext().put("pdList", pdList);
		return updateUI;
	}
	
	public String update(){
		FormTemplate formTemplate = new FormTemplate();
		String url = UploadUtils.saveUploadFile(this.resource,"/WEB-INF/upload");
		BeanUtils.copyProperties(this.getModel(), formTemplate);
		formTemplate.setUrl(url);
		this.formTemplateService.updateFormTemplate(formTemplate);
		return action2action;
	}
	public String delete(){
		this.formTemplateService.deleteFormTemplate(this.getModel().getFtid());
		return action2action;
	}
	
	public String download() throws Exception{
	    this.inputStream = this.formTemplateService.download(this.getModel().getFtid());
		return "download";
	}
}
